package com.haochang.chunkvod.controller.adapter.order;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.config.ApiConfig;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.fragment.order.TakeOutHistoryFragment;
import com.haochang.chunkvod.model.order.FoodDetailInfo;
import com.haochang.chunkvod.model.order.FoodInfo;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.model.order.query.OrderInfo;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpMethodEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.httpenum.HttpRequestLoadingEnum;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestBuilder;
import com.haochang.chunkvod.tools.thirdparty.network.http.request.HttpRequestTask;
import com.haochang.chunkvod.tools.vod.other.AppendImageUtil;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.activity.AuthorizationDialogActivity;
import com.haochang.chunkvod.view.vod.activity.CustomDialogActivity;
import com.haochang.chunkvod.view.vod.popupWindow.IPopWindowLongClickListener;
import com.haochang.chunkvod.view.vod.spinner.OperateView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * 外带--历史
 *
 * @author Yangjie
 *         className OrderQueryTakeOutAdapter
 *         created at  2016/10/25  20:32
 */

public class OrderQueryTakeOutAdapter extends BaseAdapter implements IPopWindowLongClickListener
{
	private Context context;
	private TakeOutHistoryFragment fragment;
	private List<OrderInfo> infoList = new ArrayList<OrderInfo>();
	private GoodsInfo goodsInfo = new GoodsInfo();
	private OperateView currentView = null;
	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
	private int keyOfItemTag = R.id.clicked_item;
	private int keyOfView = R.id.item_view;//点击传递一个view过去

	public OrderQueryTakeOutAdapter(TakeOutHistoryFragment fragment, Context context)
	{
		this.fragment = fragment;
		this.context = context;
	}

	public void setData(List<OrderInfo> list)
	{
		if (!CollectionUtils.isEmpty(list))
		{
			infoList.clear();
			infoList.addAll(list);
			parseGoodInfo();
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount()
	{
		return CollectionUtils.isEmpty(infoList) ? 0 : infoList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return CollectionUtils.isEmpty(infoList) ? null : infoList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;
		if (null == convertView)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.item_order_take_out_history, parent, false);
			holder = new ViewHolder();
			holder.tv_totalPrice = (BaseTextView) convertView.findViewById(R.id.tv_totalPrice);
			holder.tv_time = (BaseTextView) convertView.findViewById(R.id.tv_time);
			holder.lay_content_div = (LinearLayout) convertView.findViewById(R.id.lay_content_div);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		holder.lay_content_div.removeAllViews();
		OrderInfo orderInfo = infoList.get(position);
		final String flow_code = orderInfo.getFlow_code();
		holder.tv_totalPrice.setText(Html.fromHtml("<font color=#f04e39>" + SaleUtils.getSaleString(orderInfo.getTotal_price()) + "</font>"));
		holder.tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
		try
		{
			holder.tv_time.setText(TimeUtils.getTimeString24HR(simpleDateFormat.parse(orderInfo.getFlow_time()).getTime()));
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		List<GoodsInfo> infoList = orderInfo.getGoodsInfoList();
		if (!CollectionUtils.isEmpty(infoList))
		{
			int lenth = infoList.size();
			for (int i = 0; i < lenth; i++)
			{
				final GoodsInfo info = infoList.get(i);
				final ViewGroup viewGroup = null;
				View secondView = LayoutInflater.from(context).inflate(R.layout.item_order_query_second_layout, viewGroup, false);
				BaseTextView tv_foodName = (BaseTextView) secondView.findViewById(R.id.tv_foodName);
				BaseTextView tv_num_unit = (BaseTextView) secondView.findViewById(R.id.tv_num_unit);
				BaseTextView tv_totalPrice = (BaseTextView) secondView.findViewById(R.id.tv_totalPrice);
				BaseTextView tv_out_single = (BaseTextView) secondView.findViewById(R.id.tv_out_single);
				ImageView iv_expand = (ImageView) secondView.findViewById(R.id.iv_direction);//展开按钮
				final RelativeLayout title_div = (RelativeLayout) secondView.findViewById(R.id.title_div);
				final OperateView operateView = (OperateView) secondView.findViewById(R.id.operateView);
				View v_line = secondView.findViewById(R.id.v_line);
				v_line.setVisibility(i == 0 ? View.INVISIBLE : View.VISIBLE);
				if (info.is_back())//退单
				{
					tv_num_unit.setVisibility(View.VISIBLE);
					tv_foodName.setText(Html.fromHtml("<font color=#f04e39>" + info.getGoodsName() + "</font>"));
					tv_num_unit.setText(Html.fromHtml("<font color=#f04e39>" + String.valueOf(info.getOrderNum() + info.getUnit()) + "" + "</font>"));
					tv_totalPrice.setText(Html.fromHtml("<font color=#f04e39>" + SaleUtils.getSaleString(info.getPrice() * info.getOrderNum()) + "</font>"));
					tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
					tv_out_single.setVisibility(View.INVISIBLE);
				} else
				{
					if (goodsInfo.isShowOperate() && TextUtils.equals(info.getGoodsId(), goodsInfo.getGoodsId())
							&& (info.getD_id_value() + info.getOrderNum()).equals(goodsInfo.getD_id_value() + goodsInfo.getOrderNum()))
					{//退单/催单/取消赠送 等的布局
						showGoodsOperateStyle(operateView, info, flow_code, false);
					} else
					{
						operateView.setVisibility(View.GONE);
					}
					tv_foodName.setText(info.getGoodsName());
					if (info.is_give())//赠送
					{
						tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
						tv_totalPrice
								.setText(Html.fromHtml("<font color=#f04e39>" + SaleUtils.getSaleString(info.getPrice() * info.getOrderNum()) + "</font>"));
						tv_totalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); //中划线与抗锯齿
						if (!TextUtils.isEmpty(info.getBill_promotion_id()))//优惠活动赠送
							tv_foodName.append(AppendImageUtil.addImageSpan(context, R.drawable.is_active_gift, 10));
						else
							tv_foodName.append(AppendImageUtil.addImageSpan(context, R.drawable.btn, 10));
					} else
					{
						tv_totalPrice.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_gray), null, null, null);
						tv_totalPrice.setText(SaleUtils.getSaleString(info.getPrice() * info.getOrderNum()));
					}
					tv_num_unit.setText(String.valueOf(info.getOrderNum() + info.getUnit()));
					tv_num_unit.setVisibility(View.VISIBLE);
					tv_out_single.setText(info.getOutput_status() == 2 ? "已划单" : Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
					tv_out_single.setVisibility(info.is_give() ? View.INVISIBLE : View.VISIBLE);//赠送就隐藏该view
					final LinearLayout lay_content_div = (LinearLayout) secondView.findViewById(R.id.lay_content_div);
					lay_content_div.removeAllViews();
					/////////////////////////////////////////////////////
					iv_expand.setVisibility(((info.getGoods_type() == VodConstant.BUNDLE_TYPE || info.getGoods_type() == VodConstant.ACTIVE_TYPE)
							&& info.getFoodDetailInfoList().size() > 0) ? View.VISIBLE : View.GONE);
					iv_expand.setImageResource(info.isExpand() ? R.drawable.up : R.drawable.down);
					lay_content_div.setVisibility(info.isExpand() ? View.VISIBLE : View.GONE);
					iv_expand.setTag(keyOfItemTag, info);
					iv_expand.setTag(keyOfView, lay_content_div);
					iv_expand.setOnClickListener(onClickListener);
					////////////////////////////////////////////////////////
					if (info.isExpand() && info.getGoods_type() == VodConstant.BUNDLE_TYPE)//套餐开始
					{//添加判断条件，展开才执行
						tv_out_single.setVisibility(View.INVISIBLE);
						List<FoodDetailInfo> retails = info.getFoodDetailInfoList();
						if (!CollectionUtils.isEmpty(retails))
						{
							int len = retails.size();
							for (int j = 0; j < len; j++)
							{
								final FoodDetailInfo foodDetailInfo = retails.get(j);
								final ViewGroup viewGroup1 = null;
								final View fourView = LayoutInflater.from(context).inflate(R.layout.item_order_query_four_layout, viewGroup1, false);
								BaseTextView tv_four_foodName = (BaseTextView) fourView.findViewById(R.id.tv_foodName);
								BaseTextView tv_four_num_unit = (BaseTextView) fourView.findViewById(R.id.tv_num_unit);
								BaseTextView tv_four_out_single = (BaseTextView) fourView.findViewById(R.id.tv_out_single);
								View spaceLineFour = fourView.findViewById(R.id.spaceLineFour);
								RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) spaceLineFour.getLayoutParams();
								layoutParams.setMargins(DipPxConversion.dip2px(context, j == 0 ? 16 : 48), 0, 0, 0);
								spaceLineFour.setLayoutParams(layoutParams);
								final OperateView four_operateView = (OperateView) fourView.findViewById(R.id.four_operateView);
								tv_four_foodName.setText(foodDetailInfo.getName());
								tv_four_num_unit.setText(String.valueOf(foodDetailInfo.getQuantity() + foodDetailInfo.getUnit()));
								tv_four_out_single.setText(
										foodDetailInfo.getOutput_status() == 2 ? "已划单" : Html.fromHtml("<font size=6 color=#ffab40>" + "下单" + "</font>"));
								lay_content_div.addView(fourView);
								if (TextUtils.equals(foodDetailInfo.getFood_id(), goodsInfo.getGoodsId())
										&& (foodDetailInfo.getD_id_value().equals(goodsInfo.getD_id_value())) && goodsInfo.isShowOperate())
									showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, info.getIs_outposition(), true, info.is_give(),
											false, false);
								else
									four_operateView.setVisibility(View.GONE);
								//属于套餐的商品
								fourView.setOnClickListener(new OnBaseClickListener()
								{
									@Override
									public void onBaseClick(View v)
									{
										showFoodDetailOperateStyle(four_operateView, foodDetailInfo, flow_code, info.getIs_outposition(), true, info.is_give(),
												false, true);
									}
								});
							}
						}
					}
					title_div.setOnClickListener(new View.OnClickListener()
					{

						@Override
						public void onClick(View v)
						{
							showGoodsOperateStyle(operateView, info, flow_code, true);
						}
					});
				}
				holder.lay_content_div.addView(secondView);
			}
		}
		return convertView;
	}

	View.OnClickListener onClickListener = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			GoodsInfo item = (GoodsInfo) v.getTag(keyOfItemTag);
			LinearLayout linearContainer = (LinearLayout) v.getTag(keyOfView);
			if (null != item && null != linearContainer)
			{
				boolean isShown = linearContainer.isShown();//点击之前的状态
				linearContainer.setVisibility(isShown ? View.GONE : View.VISIBLE);
				item.setExpand(!isShown);
				((ImageView) v).setImageResource(isShown ? R.drawable.down : R.drawable.up);
				notifyDataSetChanged();
			}
		}
	};

	/**
	 * 展示套餐或者活动详情里面单品样式
	 *
	 * @param operateView   操作行view
	 * @param info          商品信息
	 * @param flow_code     流水号
	 * @param isOutProperty 是否有出餐点属性
	 * @param isPack        是否属于套餐
	 * @param isParentGive  活动或者套餐是否为增送
	 * @param isChange      是否更换过商品
	 * @param refreshData      是否需要刷新数据
	 */
	private void showFoodDetailOperateStyle(OperateView operateView, FoodDetailInfo info, String flow_code, int isOutProperty, boolean isPack,
			boolean isParentGive, boolean isChange, boolean refreshData)
	{
		//属于活动或者套餐的单品
		if (operateView.getVisibility() == View.GONE)
		{
			currentView = operateView;
			goodsInfo.setD_id_value(info.getD_id_value());
			goodsInfo.setOrderNum(info.getQuantity());
			goodsInfo.setGoodsId(info.getFood_id());
			goodsInfo.setShowOperate(true);
			operateView.setVisibility(View.VISIBLE);
			if (isParentGive)//活动或者套餐是赠送
			{
				if (!isPack)
					operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
				else
				{
					if (isOutProperty == 0)
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
				}
			} else //活动或者套餐不是赠送
			{
				if (isChange)
				{
					if (!isPack)
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					else
					{
						if (isOutProperty == 0)
							operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					}
				} else
				{
					if (isOutProperty == 0)//套餐是否具有出餐点属性
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
				}
			}
			operateView.setPopWindowLongClickListener(OrderQueryTakeOutAdapter.this);
			if (refreshData)
				notifyDataSetChanged();
		} else
		{
			restoreCurrentView(operateView);
		}
	}

	/**
	 * 展示商品样式
	 *
	 * @param operateView 操作行view
	 * @param info        商品信息
	 * @param flow_code   流水号
	 * @param refreshData   是否刷新数据
	 */
	private void showGoodsOperateStyle(OperateView operateView, GoodsInfo info, String flow_code, boolean refreshData)
	{
		if (info.getGoods_type() == VodConstant.RETAIL_TYPE)
		{ //不属于活动或者套餐的单品
			if (operateView.getVisibility() == View.GONE)
			{
				currentView = operateView;
				goodsInfo.setD_id_value(info.getD_id_value());
				goodsInfo.setOrderNum(info.getOrderNum());
				goodsInfo.setGoodsId(info.getGoodsId());
				goodsInfo.setShowOperate(true);
				operateView.setVisibility(View.VISIBLE);
				if (info.is_give())
				{
					if (!TextUtils.isEmpty(info.getBill_promotion_id()))//优惠活动赠送
						operateView.initViewData(OperateView.DialogEnum.SINGLE_REMINDER, flow_code, info);
					else
						operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_CANCAL, flow_code, info);
				} else
					operateView.initViewData(OperateView.DialogEnum.DOUBLE_REMINDER_BACK, flow_code, info);
				operateView.setPopWindowLongClickListener(OrderQueryTakeOutAdapter.this);
				if (refreshData)
					notifyDataSetChanged();
			} else
			{
				restoreCurrentView(operateView);
			}
		} else if (info.getGoods_type() == VodConstant.BUNDLE_TYPE)
		{
			//不属于活动的套餐
			if (operateView.getVisibility() == View.GONE)
			{
				currentView = operateView;
				goodsInfo.setGoodsId(info.getGoodsId());
				goodsInfo.setD_id_value(info.getD_id_value());
				goodsInfo.setOrderNum(info.getOrderNum());
				goodsInfo.setShowOperate(true);
				operateView.setVisibility(View.VISIBLE);
				if (info.is_give() && TextUtils.isEmpty(info.getBill_promotion_id()))
				{
					operateView.initViewData(OperateView.DialogEnum.SINGLE_CANCAL, flow_code, info);
				} else if (!info.is_give())
				{
					operateView.initViewData(OperateView.DialogEnum.SINGLE_BACK, flow_code, info);
				}
				operateView.setPopWindowLongClickListener(OrderQueryTakeOutAdapter.this);
				if (refreshData)
					notifyDataSetChanged();
			} else
			{
				restoreCurrentView(operateView);
			}
		} else
		{
			if (info.is_give())//活动
			{
				if (operateView.getVisibility() == View.GONE)
				{
					currentView = operateView;
					goodsInfo.setGoodsId(info.getGoodsId());
					goodsInfo.setD_id_value(info.getD_id_value());
					goodsInfo.setOrderNum(info.getOrderNum());
					goodsInfo.setShowOperate(true);
					operateView.setVisibility(View.VISIBLE);
					operateView.initViewData(OperateView.DialogEnum.SINGLE_CANCAL, flow_code, info);
					operateView.setPopWindowLongClickListener(OrderQueryTakeOutAdapter.this);
					if (refreshData)
						notifyDataSetChanged();
				} else
				{
					restoreCurrentView(operateView);
				}
			}
		}
	}

	/**
	 * 还原当前操作行数据
	 */
	private void restoreCurrentView(OperateView operateView)
	{
		currentView = null;
		goodsInfo.setGoodsId("");
		goodsInfo.setD_id_value("");
		goodsInfo.setOrderNum(0);
		goodsInfo.setShowOperate(false);
		operateView.setVisibility(View.GONE);
	}

	/**
	 * 隐藏操作行
	 */
	public void hideOperateView()
	{
		if (currentView != null)
		{
			currentView.setVisibility(View.GONE);
			goodsInfo.setD_id_value("");
			goodsInfo.setOrderNum(0);
			goodsInfo.setShowOperate(false);
		}
	}

	/**
	 * 取消赠送
	 */
	@Override
	public void cancelGive(Object object, String flow_code)
	{
		hideOperateView();
		GoodsInfo info = (GoodsInfo) object;
		Intent intent = new Intent(context, AuthorizationDialogActivity.class);
		intent.putExtra(IntentKey.GOODSINFO, info);
		intent.putExtra(IntentKey.FLOW_CODE, flow_code);
		intent.putExtra(IntentKey.TOKEN_EVENT, "门店POS系统>点餐系统模块>点餐>外带取消赠送");//传入接口事件名
		intent.putExtra(IntentKey.INTERFACE_NAME, ApiConfig.VOD_TOKEN);//传入接口名字
		fragment.startActivityForResult(intent, IntentCode.ORDER_SEARCH_REQUEST_GIVE);
	}

	/**
	 * 退单
	 *
	 * @param object    实体
	 * @param flow_code flow_code
	 */
	@Override
	public void back(Object object, String flow_code)
	{
		hideOperateView();
		GoodsInfo info = (GoodsInfo) object;
		int num = info.getRestQuantity();//剩余数量
		if (num <= 0)
		{
			ToastUtils.showText(context, "该商品已退单");
			return;
		}
		Intent intent = new Intent(context, CustomDialogActivity.class);
		intent.putExtra(IntentKey.GOODSINFO, info);
		intent.putExtra(IntentKey.FLOW_CODE, flow_code);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, info.getGoodsName());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, String.valueOf(num));
		intent.putExtra(IntentKey.UNIT, info.getUnit());
		intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "退单数量");
		intent.putExtra(IntentKey.MAX_BACK_NUMBER, num);
		intent.putExtra(IntentKey.CUSTOM_DIALOG_TYPE, CustomDialogActivity.DialogEnum.CUSTOM_VIEW_BACK.toString());
		intent.putExtra(IntentKey.OUTPUT_STATUS, info.getOutput_status());
		fragment.startActivityForResult(intent, IntentCode.ORDER_SEARCH_REQUEST_BACK);
	}

	@Override
	public void reminder(Object object, String flow_code)
	{
		hideOperateView();
		if (object instanceof GoodsInfo)//第一层   ----最外层点击
		{
			GoodsInfo goodsInfo = (GoodsInfo) object;
			if (goodsInfo.getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else if (goodsInfo.getRestQuantity() <= 0)
			{
				ToastUtils.showText(context, "商品已退单");
			} else
			{
				reminder(goodsInfo.getGoodsName(), goodsInfo.getD_id_value());
			}
		} else if (object instanceof FoodInfo)//第二层
		{
			if (((FoodInfo) object).getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else
				reminder(((FoodInfo) object).getName(), ((FoodInfo) object).getD_id_value());
		} else
		{
			if (((FoodDetailInfo) object).getOutput_status() == 2)
				ToastUtils.showText(context, "商品已划单");
			else
				reminder(((FoodDetailInfo) object).getName(), ((FoodDetailInfo) object).getD_id_value());
		}
	}

	@Override
	public void change(Object obj, String flow_code)
	{
	}

	/**
	 * 催单
	 *
	 * @param name 商品名字
	 */
	private void reminder(String name, final String outer_retail_id)
	{
		hideOperateView();
		new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure).cancelable(false)
				.contentName("确定要对" + name + "催单吗？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
				{
					@Override
					public void onOnlyCancelClick()
					{

					}

					@Override
					public void onOkClick()
					{
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("outer_retail_id", outer_retail_id);
						HttpRequestTask httpRequestTask = new HttpRequestBuilder(context).interfaceName(ApiConfig.TAKE_OUT_REMINDER).param(map)
								.httpMethodEnum(HttpMethodEnum.POST).httpRequestLoadingEnum(HttpRequestLoadingEnum.HTTP_LOADING_NONE)
								.httpRequestSucessListener(new HttpRequestBuilder.IHttpRequestSucessListener()
								{
									@Override
									public void onResponseSucess(JSONObject jsonObject)
									{
										ToastUtils.showText(context, "催单成功");
									}
								}).build();
						httpRequestTask.execute();
					}

					@Override
					public void onCancelClick()
					{

					}
				}).build().show();
	}

	protected class ViewHolder
	{
		protected BaseTextView tv_totalPrice;
		protected BaseTextView tv_time;
		protected LinearLayout lay_content_div;
	}

	/**
	 * 解析 goodInfo
	 */
	private void parseGoodInfo()
	{
		for (int i = 0; i < infoList.size(); i++)
		{
			OrderInfo info = infoList.get(i);
			info.setGoodsInfoList(parseGoodInfo(info));
		}
	}

	/**
	 * 解析点餐--外带--商品
	 */
	private List<GoodsInfo> parseGoodInfo(OrderInfo info)
	{
		int totalPrice = 0;//orderInfo的总价
		List<GoodsInfo> allList = new ArrayList<GoodsInfo>();
		List<GoodsInfo> retails = new ArrayList<GoodsInfo>();
		List<GoodsInfo> bundles = new ArrayList<GoodsInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getRetails()))
			{
				JSONArray array = new JSONArray(info.getRetails());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoods_type(0);
						JSONObject object = array.getJSONObject(i);

						goodsInfo.setRestQuantity(object.optInt("rest_quantity"));
						goodsInfo.setGoodsId(object.optString("goods_id"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setGoodsName(object.optString("cn_name"));
						goodsInfo.setIs_give(!(object.optInt("is_present") == 0));
						goodsInfo.setIs_outposition(object.optInt("have_meal_device"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setOutput_status(object.optInt("output_status"));
						goodsInfo.setUnit(object.optString("sale_unit"));
						goodsInfo.setD_id_key(object.optString("d_id_key"));
						goodsInfo.setD_id_value(object.optString("id"));//外带每个单品对应的 outer_retail_id
						goodsInfo.setBill_promotion_id(object.optString("bill_promotion_id"));
						if (object.optInt("is_present") == 0)//不是赠送
							totalPrice += object.optInt("price") * object.optInt("quantity");//如果quantity<0就是退单
						goodsInfo.setIs_back(object.optInt("quantity") < 0);//这个是退单的
						retails.add(goodsInfo);
					}
				}
			}
			if (!TextUtils.isEmpty(info.getBundles()))
			{
				JSONArray array = new JSONArray(info.getBundles());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						GoodsInfo goodsInfo = new GoodsInfo();
						goodsInfo.setGoods_type(1);
						JSONObject object = array.getJSONObject(i);

						goodsInfo.setRestQuantity(object.optInt("rest_quantity"));
						goodsInfo.setGoodsId(object.optString("goods_id"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setGoodsName(object.optString("cn_name"));
						goodsInfo.setIs_give(!(object.optInt("is_present") == 0));
						goodsInfo.setIs_outposition(object.optInt("have_meal_device"));
						goodsInfo.setPrice(object.optInt("price"));
						goodsInfo.setOrderNum(object.optInt("quantity"));
						goodsInfo.setOutput_status(object.optInt("output_status"));
						goodsInfo.setUnit(TextUtils.equals(object.optString("sale_unit"), "0") ? "套" : object.optString("sale_unit"));
						goodsInfo.setD_id_key(object.optString("d_id_key"));
						goodsInfo.setD_id_value(object.optString("id"));//外带每个套餐对应的 outer_retail_id
						goodsInfo.setBill_promotion_id(object.optString("bill_promotion_id"));
						if (object.optInt("is_present") == 0)//不是赠送
							totalPrice += object.optInt("price") * object.optInt("quantity");//如果quantity<0就是退单
						goodsInfo.setIs_back(object.optInt("quantity") < 0);//这个是退单的
						JSONArray retailsArray = object.optJSONArray("goods");
						if (retailsArray != null && retailsArray.length() > 0)
						{
							goodsInfo.setRetails(retailsArray.toString());
							goodsInfo.setFoodDetailInfoList(parseFoodDetailInfo(goodsInfo));
						}
						bundles.add(goodsInfo);
					}
				}
			}
			info.setTotal_price(totalPrice);
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		if (!CollectionUtils.isEmpty(retails))
		{
			allList.addAll(retails);
		}
		if (!CollectionUtils.isEmpty(bundles))
		{
			allList.addAll(bundles);
		}
		return allList;
	}

	/**
	 * 解析套餐详情里面的商品
	 *
	 * @param info 商品信息
	 * @return 套餐信息列表
	 */
	private List<FoodDetailInfo> parseFoodDetailInfo(GoodsInfo info)
	{
		List<FoodDetailInfo> retails = new ArrayList<FoodDetailInfo>();
		try
		{
			if (!TextUtils.isEmpty(info.getRetails()))
			{
				JSONArray array = new JSONArray(info.getRetails());
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						FoodDetailInfo foodDetailInfo = new FoodDetailInfo();
						JSONObject object = array.getJSONObject(i);
						foodDetailInfo.setFood_id(object.optString("goods_id"));
						foodDetailInfo.setName(object.optString("cn_name"));
						foodDetailInfo.setPrice(object.optInt("price"));
						foodDetailInfo.setQuantity(object.optInt("quantity"));
						foodDetailInfo.setOutput_status(object.optInt("output_status"));
						foodDetailInfo.setUnit(object.optString("sale_unit"));
						foodDetailInfo.setD_id_key(object.optString("d_id_key"));
						foodDetailInfo.setD_id_value(object.optString("id"));//外带每个套餐对应的 outer_retail_id

						retails.add(foodDetailInfo);
					}
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return retails;
	}
}
